﻿package com.tvein.sanguo.swc.action.command
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.Slider;
	import fl.controls.dataGridClasses.*;
	import fl.controls.DataGrid;
	import fl.data.DataProvider;
	import fl.events.ListEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class Command5 extends MovieClip
	{
		public static const NAME:String = "Command-5";
		
		private var effectVol:int = 100;
		private var overLay:OverLay;
		
		private var targetID:int;
		private var roleEnergy:int;
		private var roleEnergyAdd:int;
		private var roleSkillRank:int;
		
		private var productionID:int;
		private var maxTime:int;
		private var timeWage:int;
		private var workList:DataGrid;
		private var timeSlider:Slider;
		
		public function Command5()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();			
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtWage.border = true;
			txtWage.borderColor = 0x333333;
			
			workList = new DataGrid();
			workList.x = 5;
			workList.y = 70;
			workList.width = 590;
			workList.height = 190;
			this.addChild(workList);
			
			timeSlider = new Slider();
			timeSlider.x = 250;
			timeSlider.y = 330;
			timeSlider.width = 240;
			timeSlider.height = 3;
			timeSlider.maximum = 100;
			timeSlider.minimum = 0;
			timeSlider.snapInterval = 10;
			timeSlider.tickInterval = 10;
			timeSlider.enabled = false;
			this.addChild(timeSlider);
			
			btnStart.visible = false;
			btnTimeMinus.visible = false;
			btnTimePlus.visible = false;
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnStart.addEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setTargetId":
					targetID = params.TargetId;
					break;
				case "setData":
					setData(params);
					reRoad();
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			var style:TextFormat = new TextFormat();
			style.color = 0xff6600;
			style.size = 16;
			style.font = "LiSu";
			
			workList.setRendererStyle("textFormat", style);
			workList.addEventListener(ListEvent.ITEM_CLICK, onSelected);
			
			var c0:DataGridColumn = new DataGridColumn("PID");
			var c1:DataGridColumn = new DataGridColumn("RoleID");
			var c2:DataGridColumn = new DataGridColumn("货主");
			var c3:DataGridColumn = new DataGridColumn("类别");
			var c4:DataGridColumn = new DataGridColumn("需时");
			var c5:DataGridColumn = new DataGridColumn("工价");
			
			c0.visible = false;
			c1.visible = false;
			c2.width = 100;
			c3.width = 100;
			c4.width = 290;
			c5.width = 100;
			
			workList.addColumn(c0);
			workList.addColumn(c1);
			workList.addColumn(c2);
			workList.addColumn(c3);
			workList.addColumn(c4);
			workList.addColumn(c5);
			
			for (var i:int = 0; i < params.length; i++)
			{
				if (params[i].EnergyNum)
				{
					roleEnergy = params[i].EnergyNum;
				}
				
				if (params[i].EnergyAdd)
				{
					roleEnergyAdd = params[i].EnergyAdd;
				}
				
				if (params[i].skillRank)
				{
					roleSkillRank = params[i].skillRank;
				}
				
				if (params[i].type)
				{
					var workType:String;
					
					if (params[i].type == 1)
					{
						workType = "私产";
					}
					else
					{
						workType = "公干";
					}
					
					workList.addItem({"PID":params[i].productionID, "RoleID":params[i].roleID, "货主":params[i].roleName, "类别":workType, "需时":params[i].needTime, "工价":params[i].timeWage});
				}
			}
		}
		
		private function reRoad():void
		{
			if (timeSlider.value == 0)
			{
				txtTime.text = "";
				btnTimeMinus.visible = false;
				btnStart.visible = false;
			}
			else
			{
				txtTime.text = "（" + timeSlider.value / 10 + " 小时）";
				btnTimeMinus.visible = true;
				
				if (txtWage.text != "")
				{
					btnStart.visible = true;
				}
			}
			
			if ((timeSlider.value * roleSkillRank / 10) < maxTime)
			{
				if (timeSlider.value + 10 <= (roleEnergy + roleEnergyAdd))
				{
					btnTimePlus.visible = true;
				}
				else
				{
					btnTimePlus.visible = false;
				}
			}
			else
			{
				btnTimePlus.visible = false;
			}
			
			txtWage.text = "（" + (timeSlider.value * roleSkillRank / 10) * timeWage + " 钱）";
		}
		
		private function onSelected(evt:ListEvent):void
		{
			productionID = evt.item["PID"];
			maxTime = evt.item["需时"];
			timeWage = evt.item["工价"];
			
			txtWage.text = "";
			timeSlider.value = 0;
			
			reRoad();
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "Command-5"]));
					break;
				case "btnStart":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["SkillCall(4,SkillUse," + targetID + ",productionID=" + productionID + "&actTime=" + timeSlider.value * 6 + ");"]]));
					break;
				case "btnTimeMinus":
					timeSlider.value -= 10;
					reRoad();
					break;
				case "btnTimePlus":
					timeSlider.value += 10;
					reRoad();
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnStart.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}